package collectionTips;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/*
*
* Collections 工具类
* */
public class CollectionsDemo {
    public static void main(String[] args) {
        Student s1 = new Student(1,"小亮",99);
        Student s2 = new Student(2,"小强",56);
        Student s3 = new Student(3,"小响",90);
        Student s4 = new Student(4,"小勇",75);
        ArrayList<Student> list = new ArrayList<Student>();
        //添加元素
        Collections.addAll(list,s1,s2,s3,s4);
        list.forEach(System.out::println);
        System.out.println("=========================");
        //打乱顺序
        Collections.shuffle(list);
        list.forEach(System.out::println);
        System.out.println("=========================");
        //方法一：在自定义类中加入比较器：按照分数
        /*结果：
            Student{id=2, name='小强', score=56.0}
            Student{id=4, name='小勇', score=75.0}
            Student{id=3, name='小响', score=90.0}
            Student{id=1, name='小亮', score=99.0}
        */

        Collections.sort(list, (o1, o2) -> o2.getId() - o1.getId());
        list.forEach(System.out::println);
    }
}
